package com.example.photoview;

import android.app.DownloadManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


import com.example.photoview.util.HttpUtil;
import com.example.photoview.util.LogUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


import javax.net.ssl.HttpsURLConnection;

import okhttp3.Call;
import okhttp3.Response;


public class ImageViewDemo extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            // 在这里刷新线程
            android.widget.ImageView iv = (android.widget.ImageView) findViewById(R.id.iv);
              iv.setImageBitmap((Bitmap) msg.obj);
        }
    };

    public void clickBtn(View v) {
//        android4.0 以后不允许主线程直接访问网络 需要new thread
        httpsURLConnection();

//        sendRequestWithOkHttp();
    }

    // 使用httpsURLConnection
    private void httpsURLConnection() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpsURLConnection connection = null;
                String path = "https://www.baidu.com";
                // 发送http请求
                try {
                    // 1.创建URL 对象
                    URL url = new URL(path);
                    // 2.获取连接对象
                    connection = (HttpsURLConnection) url.openConnection();
                    // 3.设置请求参数
                    connection.setRequestMethod("GET");   // 请求方式
                    connection.setConnectTimeout(8000);    // 设置超时时间
                    connection.setReadTimeout(8000);   // 读取超时（连接成功下载的过程）
                    // 4. 发送请求建立连接
                    connection.connect();
                    if (connection.getResponseCode() == 200) {   // 判断返回码
                        // 获取服务器返回的客户端请求的数据
                        InputStream inputStream = connection.getInputStream();
                        // 读取流中的图片，构造出一张图片
                        Bitmap bt = BitmapFactory.decodeStream(inputStream);

                        // 当子线程需要刷新ui时，需要发送一条信息到消息队列
                        Message msg = new Message();
                        // 消息对象可以携带属性
                        msg.obj = bt;
                        handler.sendMessage(msg);

                    } else {

                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    // okhttp
    public void sendRequestWithOkHttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
//                OkHttpClient client = new OkHttpClient();
//                Request request = new Request.Builder().url("http://192.168.25.185/dd.jpg").build();
//                try {
//                    Response response = client.newCall(request).execute();
//                    String s = response.body().string();
//                    LogUtil.d(s.toString());
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
                HttpUtil.sendHttpRequest("http://192.168.25.185/dd.jpg",new okhttp3.Callback(){

                    @Override
                    public void onFailure(Call call, IOException e) {
                        // 异常时返回
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        // 得到服务器返回结果
                        LogUtil.d(response.toString());

                    }
                });
            }

        }).start();
    }


}
